import json
from datetime import date, datetime

message = {100:'ok',
            101:'request failed',
            102:'method not allowed',
            103:'parameter error',
            104:'username or password error',
            105:'server internal error',
            106:'timeout',
            107:'connection error',
            108:'verify expired',
            109:'no data',
            110:'captcha parse error',
            111:'no data or file upload',
            112:'wrong html file(parse html error)'}

# return the json result
def result(code=109,result=[]):
    data = {'code':code,'message':message.get(code),'result':result}
    return json.dumps(data,cls=CJsonEncoder,sort_keys=True,ensure_ascii = False)


class CJsonEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, date):
            return obj.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self, obj)
